﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.StoreClerk
{
    public partial class ViewRetrievalForm : System.Web.UI.Page
    {
        ConsolidateRetrivalControl control;
        int catid;
        protected void Page_Load(object sender, EventArgs e)
        {
           List<Category> categoryList = new List<Category>();            
           control = new ConsolidateRetrivalControl();
           if (!IsPostBack)
           {
               categoryList = control.getAllCategory();
               ddCategory.DataSource = categoryList;
               ddCategory.DataTextField = "categoryName";
               ddCategory.DataValueField = "categoryId";
               ddCategory.DataBind();

               gvRetrievalList.DataSource = null;
               gvRetrievalList.DataBind();
           }
           else
           {
               //getting retrivial list by category id
               catid = Convert.ToInt32(ddCategory.SelectedItem.Value);
               List<Retrival> retrivals = control.getRetrievalList(catid);
               //gvRetrievalList.DataSource = retrivals;

             
               List<Retrival> retrivalResultList = new List<Retrival>();
               List<ConsolidateRetrivalItem> resultList = new List<ConsolidateRetrivalItem>();
               if(retrivals!=null)
               foreach (Retrival r in retrivals)
               {
                   int totneededqty=0;
                   resultList = control.getDataForAssignment(r.ItemId);
                   foreach(ConsolidateRetrivalItem it in resultList){
                       totneededqty += it.TotalReqQty;
                   }
                   r.RequestedQty = totneededqty;
                   if (resultList != null)
                   {
                        if (resultList.Count != 0)
                        retrivalResultList.Add(r);
                   }
               }




               if (retrivalResultList == null)
                   gvRetrievalList.DataSource = null;
               else
               {
                   foreach (Retrival r in retrivalResultList)
                   {
                       r.OutstandingQty = r.RequestedQty - r.ReceivedQty;
                   }
                   gvRetrievalList.DataSource = retrivalResultList;
               }
                   gvRetrievalList.DataBind();
                   ViewState["gvRetrievalList"] = retrivalResultList;
               
               //Label5.Text = retrivalList.Count.ToString();
           }
        }

        protected void gvRetrievalList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRetrievalList.PageIndex = e.NewPageIndex;
            gvRetrievalList.DataSource = ViewState["gvRetrievalList"];
            gvRetrievalList.DataBind();
        }
   }
}